package cn.ven.controller;

import cn.ven.entity.MyUser;
import cn.ven.service.LargeFileService;
import cn.ven.service.UserService;
import cn.ven.tool.RespJson;
import cn.ven.tool.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.websocket.server.PathParam;
import java.util.Map;

@Controller
@RequestMapping("/api/fore/user")
public class ForeUserController {

    @Autowired
    UserService userService;

    @Autowired
    LargeFileService largeFileService;

    //修改用户密码
    @RequestMapping("/updatePwd")
    @ResponseBody
    public Map<String,Object> updatePwd(String password){
        MyUser user =new MyUser(); //创建一个用户对象
        user.setPassword(password);
        user.setId(Token.getUid());
        userService.updatePwd(user);
      return   RespJson.pack(200,"更新成功",null);
    }

    //更新用户图片
    @RequestMapping("/updateImg")
    @ResponseBody
    public Map<String,Object> updateImg(@RequestParam(value = "pic",required = false)MultipartFile file) throws Exception {
        MyUser user = userService.findById(Token.getUid());//查找到对应的MyUser对象
        largeFileService.delLarge(user.getImg());//删除原来的图片
        String img = largeFileService.add(file);//添加图片
        user.setImg(img);
        userService.updateImp(user);
       return RespJson.pack(200,"更新用户头像成功",null);
    }

    //更新用户用户名和电话
    @RequestMapping("/updateInfo")
    @ResponseBody
    public Map<String,Object> updateInfo(String nickname,String phone){
        MyUser user = userService.findById(Token.getUid());
        user.setNickname(nickname);
        user.setPhone(phone);
        userService.updateInfo(user);
        return RespJson.pack(200,"更新成功",null);
    }




}
